package com.goodsogood.union.auth.sync.util;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

/**
 *作者：liyutang
 *功能描述：MD5加密工具
 *日期：2015年11月26日 下午2:04:50
 */
public class MD5Util {
    private synchronized static byte[] encode(String origin) {
        byte[] hash = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.reset();
            hash = md.digest(origin.getBytes());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return hash;
    }
    public synchronized static String toMD5(String origin) {
        String result = "";
        byte[] hash = MD5Util.encode(origin);
        for (int i = 0; i < hash.length; i++) {
            int itemp = hash[i]&0xFF;
            if(itemp<16) result += "0";
            result += Integer.toString(itemp, 16);
        }

        return result.toUpperCase();
    }
    public synchronized static String toMD5LowerCase(String origin) {
        String result = "";
        byte[] hash = MD5Util.encode(origin);
        for (int i = 0; i < hash.length; i++) {
            int itemp = hash[i]&0xFF;
            if(itemp<16) result += "0";
            result += Integer.toString(itemp, 16);
        }

        return result.toLowerCase();
    }
    public synchronized static boolean isPassword(String origin, String result) {
        if (MD5Util.toMD5(origin).equals(result)) {
            return true;
        }
        return false;
    }
    public static Map<Integer, Integer> getCharMaps(String s) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        int i = 0;
        int num = 0;
        while(s.indexOf('.')>0){
        	num = num + s.indexOf('.');
        	map.put(++i, num);
        	s = s.substring(s.indexOf('.')+1, s.length());
        }
        return map;
    }
    public static void main(String[] args){
//    	System.out.println(toMD5("123456"));
    	System.out.println(toMD5("liyan&1234456"));
//    	System.out.println(toMD5(toMD5("123456")));
//    	System.out.println(toMD5(toMD5(toMD5("123456"))));
    }
}
